https://leetcode-cn.com/problems/number-of-closed-islands/

class Solution {
public:
    //笨方法：先把周围一圈是岛屿的全部变成水域，然后再去遍历
    int v_x[4] = {1,0,-1,0};
    int v_y[4] = {0,1,0,-1};
    int grid_n = 0;
    int grid_m = 0;
    //这是一个非常基础的dfs
    void dfs(vector<vector<int>>& grid,int x,int y)
    {
        grid[x][y] = 1;
        for(int i = 0 ; i < 4;i++)
        {
            int nx = x + v_x[i];
            int ny = y + v_y[i];
            if(nx < 0 || ny < 0 || nx >= grid_n || ny >= grid_m) continue;
            if(grid[nx][ny] == 0)
            {
                dfs(grid,nx,ny);
            }
        }
    }

    int closedIsland(vector<vector<int>>& grid) {
        grid_n = grid.size();
        if(grid_n == 0) return 0;
        grid_m = grid[0].size();
        //第一遍遍历先把周围一圈边界的岛屿全部变成水域
         for(int i = 0 ; i < grid_n ; i++)
        {
            for(int j = 0; j < grid_m ; j++)
            {
                if(i == 0 || j==0 || i == grid_n - 1 || j == grid_m - 1)
                {
                    if(grid[i][j] == 0)
                        dfs(grid,i,j);
                }
            }
        }
         //第二遍遍历才是正式开始统计
        int count = 0;
        for(int i = 0 ; i < grid_n ; i++)
        {
            for(int j = 0; j < grid_m ; j++)
            {         
                if(grid[i][j] == 0)
                {
                    count++;
                    dfs(grid,i,j);
                }
            }
        }
        return count;
    }
};
